package org.seven.jrdp.biz.sys.service;

import java.util.Map;
import java.util.Set;

import org.seven.jrdp.biz.sys.mapper.SysCacheMapper;
import org.seven.jrdp.biz.sys.model.SysCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class SysCacheService {
	@Autowired
	private SysCacheMapper sysCacheMapper;

	@Transactional(readOnly = true)
	public String get(String key) {
		SysCache sysCache = sysCacheMapper.selectByPrimaryKey(key);
		if (sysCache == null) {
			return null;
		}
		return sysCache.getValue();
	}

	@Transactional
	public void put(String key, String value) {
		SysCache sysCache = new SysCache();
		sysCache.setKey(key);
		sysCache.setValue(value);
		if (get(key) == null) {
			sysCacheMapper.insertSelective(sysCache);
		} else {
			sysCacheMapper.updateByPrimaryKeySelective(sysCache);
		}
	}

	@Transactional
	public void putAll(Map<String, String> map) {
		Set<String> set = map.keySet();
		for (String key : set) {
			put(key, map.get(key));
		}
	}

	@Transactional
	public void evict(String key) {
		sysCacheMapper.deleteByPrimaryKey(key);
	}
}